import { App } from 'vue';
import themeTool from './libs/theme/theme';
import fontJson from './libs/tmicon/fontJson';
import { language } from './libs/language/language';
import { share } from './libs/mixin/share';

import http from './libs/request/index';
import toast from './libs/function/toast';
import alert from './libs/function/alert';
import route from './libs/function/route';
import queryParams from './libs/function/queryParams';
import { storage } from './libs/function/storage';
import guid from './libs/function/guid';
import random from './libs/function/random';
import debounce from './libs/function/debounce';
import throttle from './libs/function/throttle';
import deepClone from './libs/function/deepClone';
import callPhone from './libs/function/callPhone';
import scanCode from './libs/function/scanCode';
import clipboard from './libs/function/clipboard';
import array from './libs/function/array';
import time from './libs/function/time';
import test from './libs/function/test';
import window from './libs/function/window';

import PageJsonInit from '../pages.json';

const pages: Array<pagesType> = [];
if (typeof PageJsonInit?.pages == 'undefined') {
    PageJsonInit.pages = [];
}
PageJsonInit.pages.forEach(el => {
    const customType: pagesCustomType = <pagesCustomType>(el?.style?.navigationStyle ?? 'default');
    const bg =
        (el.style?.navigationBarBackgroundColor ??
            PageJsonInit?.globalStyle?.navigationBarBackgroundColor ??
            '#FFFFFF') ||
        '#FFFFFF';
    const txtColor =
        (el.style?.navigationBarTextStyle ?? PageJsonInit?.globalStyle?.navigationBarTextStyle ?? 'black') || 'black';
    pages.push({
        path: el.path,
        custom: customType,
        navigationBarBackgroundColor: bg,
        navigationBarTextStyle: txtColor,
    });
});
if (Array.isArray(PageJsonInit?.subPackages ?? null)) {
    PageJsonInit?.subPackages.forEach((el: any) => {
        const rootPath = el.root;
        el.pages.forEach((el2: any) => {
            const elany: any = el2;
            const bg =
                (el2.style?.navigationBarBackgroundColor ??
                    PageJsonInit?.globalStyle?.navigationBarBackgroundColor ??
                    '#FFFFFF') ||
                '#FFFFFF';
            const txtColor =
                (el2.style?.navigationBarTextStyle ?? PageJsonInit?.globalStyle?.navigationBarTextStyle ?? 'black') ||
                'black';
            pages.push({
                path: rootPath + '/' + elany.path,
                custom: elany?.style?.navigationStyle ?? 'default',
                navigationBarBackgroundColor: bg,
                navigationBarTextStyle: txtColor,
            });
        });
    });
}
const pagers: any = PageJsonInit;
const tabBar: tabBarType = pagers?.tabBar ?? {
    color: '',
    selectedColor: '',
    borderStyle: '',
    backgroundColor: '',
    list: [],
};

// custom icon
let cusutomIconList = [];

// #ifdef APP
cusutomIconList = fontJson;
// #endif

const api: any = {};

const $u = {
    api,
    toast,
    queryParams,
    guid,
    random,
    debounce,
    throttle,
    deepClone,
    callPhone,
    scanCode,
    window,
    clipboard,
    array,
    time,

    test,
    route,
    http,
    alert,
    storage,

    isColor: themeTool.isCssColor,
    language: language,
    pages: pages,
    tabBar: tabBar,
    tmicon: [
        {
            font: 'tmicon',
            prefix: 'tmicon-',
            fontJson: cusutomIconList,
        },
    ],
};

uni.$u = $u;

export default {
    install: (app: App, options: object) => {
        // 小程序模式:微信分享配置
        let appConfig = {};

        // #ifdef MP
        const { onShareAppMessage, onShareTimeline } = share();
        appConfig = { ...appConfig, onShareAppMessage, onShareTimeline };
        // #endif

        app.mixin({
            ...appConfig,
        });

        app.config.globalProperties.$u = $u;
    },
};
